using System.Diagnostics;

namespace ThingsGateway.Foundation
{
    /// <summary>
    /// 注入依赖对象
    /// </summary>
    [DebuggerDisplay("Lifetime = {Lifetime}, ServiceType = {ServiceType}, ImplementationType = {ImplementationType}")]
    public class ServiceDescriptor
    {

        #region Public Constructors

        /// <summary>
        /// 初始化一个全局单例实例。
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="instance"></param>
        public ServiceDescriptor(Type serviceType, object instance)
          : this(serviceType, Lifetime.Singleton)
        {
            if (serviceType == null)
                throw new ArgumentNullException(nameof(serviceType));
            ImplementationInstance = instance;
            ImplementationType = instance.GetType();
        }

        /// <summary>
        /// 初始化一个完整的服务注册
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="implementationType"></param>
        /// <param name="lifetime"></param>
        public ServiceDescriptor(Type serviceType, Type implementationType, Lifetime lifetime)
        {
            ServiceType = serviceType;
            Lifetime = lifetime;
            ImplementationType = implementationType;
        }

        /// <summary>
        /// 初始化一个简单的的服务描述
        /// </summary>
        /// <param name="serviceType"></param>
        public ServiceDescriptor(Type serviceType)
        {
            ServiceType = serviceType;
        }

        private ServiceDescriptor(Type serviceType, Lifetime lifetime)
        {
            ServiceType = serviceType;
            Lifetime = lifetime;
        }
        #endregion Public Constructors

        #region Public Properties

        /// <summary>
        /// 实例化工厂委托
        /// </summary>
        public Func<IContainer, object> ImplementationFactory { get; set; }

        /// <summary>
        /// 实例
        /// </summary>
        public object ImplementationInstance { get; set; }

        /// <summary>
        /// 实例类型
        /// </summary>
        public Type ImplementationType { get; }

        /// <summary>
        /// 生命周期
        /// </summary>
        public Lifetime Lifetime { get; }

        /// <summary>
        /// 注册类型
        /// </summary>
        public Type ServiceType { get; }

        #endregion Public Properties
    }
}